package com.ruoyi.interfaceConfig.businessObject.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaceConfig.businessObject.domain.BusinessFieldSetting;
import com.ruoyi.interfaceConfig.businessObject.domain.vo.BusinessObjectConfigVo;
import com.ruoyi.interfaceConfig.businessObject.mapper.BusinessFieldSettingMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.interfaceConfig.businessObject.mapper.BusinessObjectConfigMapper;
import com.ruoyi.interfaceConfig.businessObject.domain.BusinessObjectConfig;
import com.ruoyi.interfaceConfig.businessObject.service.IBusinessObjectConfigService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务对象配置Service业务层处理
 * 
 * @author YiYuan
 * @date 2024-02-01
 */
@Service
@RequiredArgsConstructor
public class BusinessObjectConfigServiceImpl implements IBusinessObjectConfigService 
{
    private final BusinessObjectConfigMapper businessObjectConfigMapper;
    private final BusinessFieldSettingMapper businessFieldSettingMapper;

    /**
     * 查询业务对象配置
     * 
     * @param id 业务对象配置主键
     * @return 业务对象配置
     */
    @Override
    public BusinessObjectConfigVo selectBusinessObjectConfigById(Long id)
    {
        BusinessObjectConfig businessObjectConfig = businessObjectConfigMapper.selectBusinessObjectConfigById(id);
        //根据businessId获取字段信息
        List<BusinessFieldSetting> businessFieldSettings = businessFieldSettingMapper.getFieldInfo(id);
        BusinessObjectConfigVo businessObjectConfigVo = new BusinessObjectConfigVo();
        BeanUtils.copyProperties(businessObjectConfig, businessObjectConfigVo);
        businessObjectConfigVo.setBusinessFieldSettings(businessFieldSettings);
        return businessObjectConfigVo;
    }

    /**
     * 查询业务对象配置列表
     * 
     * @param businessObjectConfig 业务对象配置
     * @return 业务对象配置
     */
    @Override
    public List<BusinessObjectConfig> selectBusinessObjectConfigList(BusinessObjectConfig businessObjectConfig)
    {
        return businessObjectConfigMapper.selectBusinessObjectConfigList(businessObjectConfig);
    }

    /**
     * 新增业务对象配置
     * 
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusinessObjectConfig(BusinessObjectConfigVo businessObjectConfigVo)
    {
        BusinessObjectConfig businessObjectConfig = new BusinessObjectConfig();
        BeanUtils.copyProperties(businessObjectConfigVo, businessObjectConfig);
        int i = businessObjectConfigMapper.insertBusinessObjectConfig(businessObjectConfig);
        Long businessId = businessObjectConfig.getId();
        List<BusinessFieldSetting> businessFieldSettings = businessObjectConfigVo.getBusinessFieldSettings();
        //批量添加字段信息
        businessFieldSettingMapper.insertBatch(businessFieldSettings, businessId);
        return i;
    }

    /**
     * 修改业务对象配置
     * 
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusinessObjectConfig(BusinessObjectConfigVo businessObjectConfigVo)
    {
        //批量删除字段信息，再批量新增
        BusinessObjectConfig businessObjectConfig = new BusinessObjectConfig();
        BeanUtils.copyProperties(businessObjectConfigVo, businessObjectConfig);
        Long businessId = businessObjectConfig.getId();
        List<BusinessFieldSetting> businessFieldSettings = businessObjectConfigVo.getBusinessFieldSettings();
        businessFieldSettingMapper.deleteBusinessFieldSettingByBusinessId(businessId);
        businessFieldSettingMapper.insertBatch(businessFieldSettings, businessId);
        return businessObjectConfigMapper.updateBusinessObjectConfig(businessObjectConfig);
    }

    /**
     * 批量删除业务对象配置
     * 
     * @param ids 需要删除的业务对象配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusinessObjectConfigByIds(Long[] ids)
    {
        //删除字段配置信息
        businessFieldSettingMapper.deleteBusinessFieldSettingByBusinessIds(ids);
        return businessObjectConfigMapper.deleteBusinessObjectConfigByIds(ids);
    }

    /**
     * 删除业务对象配置信息
     * 
     * @param id 业务对象配置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusinessObjectConfigById(Long id)
    {
        businessFieldSettingMapper.deleteBusinessFieldSettingByBusinessId(id);
        return businessObjectConfigMapper.deleteBusinessObjectConfigById(id);
    }

    @Override
    public AjaxResult getSourceAndTargetSysObject() {
        //金蝶云星空
        List<BusinessObjectConfig> sourceObject = businessObjectConfigMapper.selectBusinessObjectBySysPlatform("02");
        //序列号追溯系统
        List<BusinessObjectConfig> targetObject = businessObjectConfigMapper.selectBusinessObjectBySysPlatform("01");
        Map<String,Object> map = new HashMap<>(3,1f);
        map.put("sourceObject",sourceObject);
        map.put("targetObject",targetObject);
        return AjaxResult.success(map);
    }
}
